//
// Today.m
// Copyright (c) 1989, 1990 by Jiro Nakamura 
// All rights reserved
//
//	by Jiro Nakamura (ac6y@vax5.cit.cornell.edu)
//
// RCS Information
// Revision Number->	$Revision: 2.6 $
// Last Revised->	$Date: 90/12/03 01:56:40 $
//
static char rcsid[] = "$Id: Today.m,v 2.6 90/12/03 01:56:40 jiro Exp Locker: jiro $";

#import "Today.h"
#import "Global.h"
#import "Event.h"
#import "cass.h"	// for TODAY_ICON
#import "calendar.h"	// for ascMyDate()

#import <appkit/Application.h> 	// for NX_MODALRESPTHRESHOLD 
#import <appkit/ScrollView.h>

#import <sys/time.h>		
#import <sys/file.h>
#import <libc.h>
#import <strings.h>


@implementation Today
- open:sender
{
	static alreadyInited = FALSE;
	
	if( !alreadyInited)
		{
		alreadyInited = TRUE;
		[self placeWindow: [global todayFrame]];
		}

	[self setWindowIcon: 	TODAY_ICON];
	[self openWith: 	""];
	textView = [textScroll docView];
	[textView setDelegate : self];	
	[textView setMonoFont: FALSE];
	[textView setEditable: FALSE];
	miniaturized = FALSE;
	[self setDelegate: self];
	
	return self;
}

- save
{
	// This window shouldn't save itself. 
	[self setDocEdited: FALSE];
	[textView becomeFirstResponder];
	return self;
}

- close
{
	[global	saveThisWindowPosition:	DEFAULTTODAYFRAME : self];
	[super close];
	miniaturized = FALSE;
	return self;
}

- update
{
	NXStream *output; 
	Event *ev;
	int  today, temp;
	static struct tm time;

	if( miniaturized || [self isVisible])
		{
		time = *timeNow();

		// We want to read the eventFile pointed to by global 
		ev = [Event newAt:[global eventFile]];


		// We need a temporary memory stream to store the queue.
		if( (output = NXOpenMemory( NULL, 0, NX_READWRITE)) == NULL)
			{
			fprintf(stderr,"%s: Couldn't open memory stream for "
				"Today queue scrollview. FATAL ERRROR.\n",
				PROGNAME);
			exit(1) ;
			}
				
		[ev firstEvent];
		today = [ev mday];
		
		/* If the first event is today, then we just won't print
		   anything */
		if( ([ev mday] != time.tm_mday) ||
		    ([ev mon]  != time.tm_mon) ||
		    ([ev year] != time.tm_year))
		    	today = -1;
	
		NXPrintf(output, "{\\rtf0\\ansi{\\fonttbl\\f0\\fmodern "
				"%s;}\\fs%.0f\n",
				[global fontName], [global fontSize] * 2);

		if( [ev present] == 0 || today == -1)
			NXPrintf(output, 
				"{\\f0Nothing for today.\\par\\par}\n");
					
		/* Print all events for today regardless of priority */
		while ( [ev present] != 0 && [ev mday] == today)
			{
			if( [ev priority] > [global highPriority])
				NXPrintf(output, "{\\f0\\b ");
			else
				NXPrintf(output, "{\\f0 ");

			if( [global militaryTime])
				NXPrintf(output, "%2d:%02d  %s\\par}\n", 
					[ev hour], [ev min], 
					[ev message]);
			else
				{
				temp = [ev hour];
				if( temp > 12 ) temp -= 12;
				if( temp == 0 )
					temp = 12;			
				NXPrintf(output, "%2d:%02d %s  "
					"%s\\par}\n", 
					temp,	[ev min], 
					([ev hour]>11)?"pm":"am",
					[ev message]);
				}
			[ev readEvent : [ev next]]; 
			}   
		
		// Get ready to print everything for tomorrow
		today = [ev mday];
		time.tm_mday ++;
		fixTmStructure( &time );
			
		// If the next event isn't tomorrow, then
		// say that there is nothing tomorrow
		if( 	([ev mday] != time.tm_mday) ||
		    	([ev mon]  != time.tm_mon) ||
		  	([ev year] != time.tm_year))
		    	today = -1;
				
		NXPrintf(output, "{\\f0 -------------\\par}\n");

		if( [ev present] == 0 || today == -1)
			NXPrintf(output, 
				"{\\f0 Nothing for tomorrow.}\n");
		else
			NXPrintf(output, 
				"{\\f0\\b Tomorrow: %s\\par\\par}\n",
				ascMyDate( &time));
		
		while ( [ev present] != 0 && [ev mday] == today)
			{
			if( [ev priority] > [global highPriority])
				NXPrintf(output, "{\\f0\\b ");
			else
				NXPrintf(output, "{\\f0 ");

			if( [global militaryTime])
				NXPrintf(output, "%2d:%02d  "
					"%s\\par}\n", 
					[ev hour], [ev min], 
					[ev message]);
			else
				{
				temp = [ev hour];
			
				if( temp > 12 )
					temp -= 12;
				if( temp == 0 )
					temp = 12;			
			
				NXPrintf(output, "%2d:%02d %s  "
						"%s\\par}\n", 
						temp,
						[ev min], 
						([ev hour]>11)?
						"pm":"am",
						[ev message]);
				}

			[ev readEvent : [ev next]]; 
			}	

		NXPrintf(output, "}\n");	
		

		NXSeek(output, 0, NX_FROMSTART);
		[textView readRichText: output]; 
		NXCloseMemory(output, NX_FREEBUFFER); 
	
		[self setDocEdited: FALSE];
		[textView becomeFirstResponder];
		[textView hideCaret];	// for some reason the Text
		[textView update];	// object sometimes leaves a caret...
		
		[ev free];		
		[self timeUpdate: self];
		}	
	
	return self;
}

- timeUpdate:sender
{
	static char     *timeMark;
	static char buf[30];
	static struct tm time;
	extern const char *shortMonths[12];
	extern const char *shortWeekDays[7];

	if ( !miniaturized && [self isVisible])
	{
		time = *timeNow();
		if (!showTime)
			{
			if( !miniaturized)
				sprintf(buf, "%s - Today", ascMyDate(&time));
			else
				sprintf(buf, "%s %s %2d",
					shortWeekDays[time.tm_wday],
					shortMonths[time.tm_mon],
					time.tm_mday);
			}
		else
		{
			if( [self isVisible])
				sprintf(buf, "%s - Today", ascMyTime(&time, 
					showSeconds, militaryTime));
			else
				{	
				if( minute == time.tm_min)
					return self;
				else
					minute = time.tm_min;
						
				hour = time.tm_hour;
				if( militaryTime)
					timeMark = "";
				else
					{
					if (hour > 11)
						timeMark = "p";
					else
						timeMark = "a";
		
					if (hour > 12)
						hour -= 12;
					if (hour == 0)
						hour = 12;
					}
				sprintf(buf, "%d:%02d%s %d/%d", hour, 
					time.tm_min, timeMark, time.tm_mon+1, 
					time.tm_mday);
				}
		}
		[self setTitle:buf];
	}
	return self;
}

- defaultsDidChange: sender
{
	#ifdef DEBUG
		fprintf(stderr,"Defaults changed.... Overview compensating\n");
	#endif
	
	militaryTime 	= [global militaryTime];
	showSeconds 	= [global showSeconds];
	showTime	= [global todayShowTime];
	
	[self update];
	return self;
}

- windowDidMiniaturize: sender
{
	#ifdef DEBUG
		fprintf(stderr,"Today did miniaturize\n");
	#endif

	miniaturized = TRUE;
	minute = -1;		// Force a redisplay on reopening
	return self;
}

- windowDidDeminiaturize: sender
{
	#ifdef DEBUG
		fprintf(stderr,"Today did deminiaturize\n");
	#endif

	miniaturized = FALSE;
	minute = -1;		// Force a redisplay
	[self timeUpdate:self];
	return self;
}


-setGlobal:anObject
{
	global = anObject;
	return self;
}

@end
